package com.shxy.rlzy.mapper;

import com.shxy.rlzy.entity.SalaryItem;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
public interface SalaryItemMapper extends BaseMapper<SalaryItem> {

    /**
     * 查询部门任务员工工资列表
     * @param taskDepartId
     * @return
     */
    @Select("SELECT si.id, si.task_id, si.task_depart_id, si.salary_id, si.month_merit, si.statu, es.employee_id, ei.`name`\n" +
            "FROM emp_salary AS es, emp_info AS ei, emp_salary_item AS si, emp_salary_task_depart AS std\n" +
            "WHERE es.employee_id=ei.id AND si.salary_id=es.id AND si.task_depart_id=std.id AND si.task_depart_id=#{taskDepartId}")
    List<Map<String, Object>> getTaskDepartItems(long taskDepartId);

    /**
     * 根据编号修改绩效
     * @param id
     * @param monthMerit
     * @return
     */
    @Update("UPDATE emp_salary_item SET month_merit = #{monthMerit} WHERE id=#{id}")
    int updateBonusById(Long id, Double monthMerit);

    /**
     * 修改状态
     * @param id
     * @param statu
     * @return
     */
    @Update("UPDATE emp_salary_item SET statu=#{statu} WHERE id=#{id}")
    int changeStatus(Long id, int statu);

    /**
     * 修改部门任务下个人任务状态
     * @param taskDepartId
     * @param statu
     * @return
     */
    @Update("UPDATE emp_salary_item SET statu=#{statu} WHERE task_depart_id=#{taskDepartId}")
    int changeStatusByDepartTaskId(Long taskDepartId, int statu);

    /**
     * 统计工资任务的个人任务数量
     * @param taskId
     * @return
     */
    @Select("SELECT COUNT(id) FROM emp_salary_item WHERE task_id=#{taskId}")
    int countByTaskId(Long taskId);

    /**
     * 统计指定状态的个人任务数量
     * @param taskId
     * @param statu
     * @return
     */
    @Select("SELECT COUNT(id) FROM emp_salary_item WHERE task_id=#{taskId} AND statu=#{statu}")
    int countByTaskIdAndStatu(Long taskId, int statu);

    /**
     * 修改指定工资任务下的所有个人任务状态
     * @param taskId
     * @param statu
     */
    @Update("UPDATE emp_salary_item SET statu=#{statu} WHERE task_id=#{taskId}")
    int changeStatusOfTask(long taskId, int statu);

    /**
     * 重置部门任务下的个人任务
     * @param departTaskId
     * @param statu
     * @param bonus
     */
    @Update("UPDATE emp_salary_item SET statu=#{statu}, month_merit=#{bonus} WHERE task_depart_id=#{departTaskId}")
    void resetByDepartTaskId(Long departTaskId, int statu, double bonus);
}
